import 'package:flutter/material.dart';
import '../../basic.dart';
import 'view_state_list_model.dart';
import 'view_state_model.dart';

///状态改变样式
class ViewStateBox extends StatefulWidget {
  /// 状态model
  final ViewStateModel model;

  /// 正常显示的内容
  final WidgetBuilder child;

  ///自定义数据加载样式
  final Widget? load;

  /// 自定义空显示内容
  final Widget? empty;
  final String? emptyText; //ViewState.empty 提示文本

  /// 自定义错误显示的内容
  final Widget? error;
  final String? errorText; //ViewState.error 提示文本

  ///一直显示在顶部Widget
  final Widget? fixed; //固定

  /// 点击事件
  final VoidCallback? onTap;

  const ViewStateBox({
    Key? key,
    required this.model,
    required this.child,
    this.load,
    this.empty,
    this.emptyText,
    this.error,
    this.errorText,
    this.fixed,
    this.onTap,
  }) : super(key: key);

  @override
  _ViewStateBoxTestState createState() => _ViewStateBoxTestState();
}

class _ViewStateBoxTestState extends State<ViewStateBox> {
  @override
  Widget build(BuildContext context) {
    ViewStateBoxConfiguration? configuration = ViewStateBoxConfiguration.of(
      context,
    );

    ViewStateModel model = widget.model;

    Widget? content;
    if (model.busy) {
      if (widget.load != null) {
        content = widget.load;
      } else {
        content = configuration?.load ?? ViewStateBusyWidget();
      }
    } else if (model.empty) {
      String? emptyText = widget.emptyText ?? configuration?.emptyText;
      if (widget.empty != null) {
        content = widget.empty;
      } else if (configuration?.empty != null) {
        content = configuration?.empty?.call(
          model,
          widget.onTap ?? defOnTap,
          emptyText,
        );
      } else {
        content = ViewStateTextWidget(
          text: emptyText ?? "暂无数据",
          onTap: widget.onTap ?? defOnTap,
          textStyle: configuration?.textStyle,
        );
      }
    } else if (model.error) {
      String? errorText = widget.errorText ?? configuration?.errorText;
      if (widget.error != null) {
        content = widget.error;
      } else if (configuration?.error != null) {
        content = configuration?.error?.call(
          model,
          widget.onTap ?? defOnTap,
          errorText,
        );
      } else {
        content = ViewStateTextWidget(
          text: errorText ?? "网络繁忙,请稍后再试",
          onTap: widget.onTap ?? defOnTap,
          textStyle: configuration?.textStyle,
        );
      }
    } else {
      content = Builder(builder: widget.child);
    }
    if (widget.fixed != null) {
      return Column(
        mainAxisAlignment: MainAxisAlignment.start,
        crossAxisAlignment: CrossAxisAlignment.start,
        children: <Widget>[
          widget.fixed ?? SizedBox(),
          Expanded(child: content ?? SizedBox()),
        ],
      );
    }
    return content ?? SizedBox();
  }

  void defOnTap() {
    if (widget.model is ViewStateListModel) {
      ViewStateListModel listModel = widget.model as ViewStateListModel;
      listModel.initData();
    }
  }
}

//加载
class ViewStateBusyWidget extends StatelessWidget {
  final Widget? child;

  const ViewStateBusyWidget({Key? key, this.child}) : super(key: key);

  @override
  Widget build(BuildContext context) {
    return Center(child: child ?? CircularProgressIndicator());
  }
}

/// 中间文字样式
class ViewStateTextWidget extends StatelessWidget {
  final String text;
  final TextStyle? textStyle;
  final VoidCallback? onTap;

  ViewStateTextWidget({
    Key? key,
    required this.text,
    this.textStyle,
    this.onTap,
  });

  @override
  Widget build(BuildContext context) {
    TextStyle defaultTextStyle = TextStyle(
      fontSize: 16,
      color: HexColor('#929292'),
    );

    return Center(
      child: GestureDetector(
        behavior: HitTestBehavior.opaque,
        onTap: () => onTap?.call(),
        child: Text(text, style: textStyle ?? defaultTextStyle),
      ),
    );
  }
}

typedef ViewStateWidgetBuilder =
    Widget Function(ViewStateModel model, VoidCallback onTap, String? tips);

// view_state_box 配置
class ViewStateBoxConfiguration extends InheritedWidget {
  final Widget? load; //加载
  // final String? loadText; //加载文字
  final ViewStateWidgetBuilder? empty; //无数据
  final String emptyText; //无数据文字
  final ViewStateWidgetBuilder? error; //异常
  final String errorText; //异常文字
  final TextStyle? textStyle;

  ViewStateBoxConfiguration({
    required Widget child,
    this.load,
    // this.loadText,
    this.empty,
    this.emptyText = "暂无数据",
    this.error,
    this.errorText = "网络繁忙,请稍后再试",
    this.textStyle,
  }) : super(child: child);

  static ViewStateBoxConfiguration? of(BuildContext context) {
    return context
        .dependOnInheritedWidgetOfExactType<ViewStateBoxConfiguration>();
  }

  @override
  bool updateShouldNotify(covariant ViewStateBoxConfiguration oldWidget) {
    return load != oldWidget.load ||
        empty != oldWidget.empty ||
        emptyText != oldWidget.emptyText ||
        error != oldWidget.error ||
        errorText != oldWidget.errorText ||
        textStyle != oldWidget.textStyle;
  }
}
